//
//  FinderViewController.m
//  WangYiNews
//
//  Created by lanou3g on 15/10/10.
//  Copyright (c) 2015年 zhengming. All rights reserved.
//

#import "FinderViewController.h"
#import "FinderTableViewCell.h"

enum RowNumber{
    rowOne = 100,
    rowTwo,
    rowThree,
    rowFour,
    rowFive,
    rowSix,
    rowSeven,
    rowEight
};

@interface FinderViewController ()<UITableViewDataSource, UITableViewDelegate, SDCycleScrollViewDelegate>
//@property (weak, nonatomic) IBOutlet UIBarButtonItem *finderBarItem;

@property (nonatomic, strong) UIButton *moneyBtn;
@property (nonatomic, strong) UIButton *serverBtn;
@property (nonatomic, strong) UIButton *activityBtn;
@property (nonatomic, strong) UIButton *gameBtn;
// 用于存储当前页面显示的model
@property (nonatomic, strong) NSMutableArray *showModels;

@end

@implementation FinderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self subView];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;
    
    [[Manager share] setupFinderDataWithBlock:^{
        [self.tableView reloadData];
    }];
}

- (void)subView
{
    // 自定义bar
    self.finderBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    self.finderBar.barTintColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 40, 20)];
    label.backgroundColor = [UIColor clearColor];
    label.text = @"发现";
    label.textColor = [UIColor whiteColor];
    [self.finderBar addSubview:label];
    [self.view addSubview:self.finderBar];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"FinderTableViewCell" bundle:nil] forCellReuseIdentifier:@"Findercell"];
}

#pragma mark -自定义Button
// 该自定义button包含一个圆形image和一个label
- (void)customButton:(UIButton *)button image:(NSString *)image imageFrame:(CGRect)imageFrame labelFrame:(CGRect)labelFrame labelText:(NSString *)text textColor:(UIColor *)color textFont:(UIFont *)font
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
    [imageView sd_setImageWithURL:[NSURL URLWithString:image]];
    imageView.layer.cornerRadius = imageFrame.size.width / 2;
    imageView.layer.masksToBounds = YES;
    [button addSubview:imageView];
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    label.text = text;
    label.textColor = color;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = font;
    [button addSubview:label];
}

#pragma mark -实现代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[Manager share] finderContextArray].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FinderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Findercell" forIndexPath:indexPath];
    CGFloat tag = 100 + indexPath.row;
    
    NSArray *array = [Manager share].finderContextArray[indexPath.row]; // 对应商家的产品数组
    FinderModel *model = array[arc4random() % array.count]; // 随机选择一件产品显示出来
    
    [cell.shopBtn addTarget:self action:@selector(shopBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.shopBtn.tag = tag;
    [cell.shopBtnImgV sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    
    cell.name.text = model.name;
    
    [cell.imageBtn addTarget:self action:@selector(imageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.imageBtn.tag = tag;
    [cell.imageBtnImgV sd_setImageWithURL:[NSURL URLWithString:model.image]];
    
    cell.titleLabel.text = model.title;
    cell.detailLabel.text = model.subtitle;
    
    [cell.buyBtn addTarget:self action:@selector(buyBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.buyBtn.tag = tag;
    [cell.buyBtn.layer setMasksToBounds:YES]; // 设置按钮的圆角半径不会被遮挡
    [cell.buyBtn.layer setCornerRadius:10];
    [cell.buyBtn.layer setBorderWidth:0.3]; // 设置边界宽度
    [cell.buyBtn.layer setBorderColor:[UIColor redColor].CGColor]; // 设置边界颜色
    [cell.buyBtn setTitle:[NSString stringWithFormat:@"%@", model.btn] forState:UIControlStateNormal];
    [cell.buyBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    cell.oldPriceLabel.text = [NSString stringWithFormat:@"%@", model.subbtn];
    
    // 设置cell无选中样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 310;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSMutableArray *array = [NSMutableArray array];
    for (FinderModel *model in [Manager share].finderBannerArray) {
        [array addObject:model.image];
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    SDCycleScrollView *scrollview = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 120) imageURLStringsGroup:array];
    scrollview.autoScrollTimeInterval = 2;
    scrollview.delegate = self;
    [view addSubview:scrollview];
    
    self.moneyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.moneyBtn.frame = CGRectMake(0, 120, kScreenWidth / 4, 80);
    [self customButton:self.moneyBtn image:((FinderModel *)[Manager share].finderRecommendArray[0]).icon imageFrame:CGRectMake(self.moneyBtn.frame.size.width / 2 - 25, 5, 50, 50) labelFrame:CGRectMake(self.moneyBtn.frame.size.width / 2 - 30, 55, 60, 20) labelText:((FinderModel *)[Manager share].finderRecommendArray[0]).title textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:13]];
    
    self.serverBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.serverBtn.frame = CGRectMake(CGRectGetMaxX(self.moneyBtn.frame), CGRectGetMinY(self.moneyBtn.frame), CGRectGetWidth(self.moneyBtn.frame), CGRectGetHeight(self.moneyBtn.frame));
    [self customButton:self.serverBtn image:((FinderModel *)[Manager share].finderRecommendArray[1]).icon imageFrame:CGRectMake(self.moneyBtn.frame.size.width / 2 - 25, 5, 50, 50) labelFrame:CGRectMake(self.moneyBtn.frame.size.width / 2 - 30, 55, 60, 20) labelText:((FinderModel *)[Manager share].finderRecommendArray[1]).title textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:13]];
    
    self.activityBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.activityBtn.frame = CGRectMake(CGRectGetMaxX(self.serverBtn.frame), CGRectGetMinY(self.moneyBtn.frame), CGRectGetWidth(self.moneyBtn.frame), CGRectGetHeight(self.moneyBtn.frame));
    [self customButton:self.activityBtn image:((FinderModel *)[Manager share].finderRecommendArray[2]).icon imageFrame:CGRectMake(self.moneyBtn.frame.size.width / 2 - 25, 5, 50, 50) labelFrame:CGRectMake(self.moneyBtn.frame.size.width / 2 - 30, 55, 60, 20) labelText:((FinderModel *)[Manager share].finderRecommendArray[2]).title textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:13]];
    
    self.gameBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.gameBtn.frame = CGRectMake(CGRectGetMaxX(self.activityBtn.frame), CGRectGetMinY(self.moneyBtn.frame), CGRectGetWidth(self.moneyBtn.frame), CGRectGetHeight(self.moneyBtn.frame));
    [self customButton:self.gameBtn image:((FinderModel *)[Manager share].finderRecommendArray[3]).icon imageFrame:CGRectMake(self.moneyBtn.frame.size.width / 2 - 25, 5, 50, 50) labelFrame:CGRectMake(self.moneyBtn.frame.size.width / 2 - 30, 55, 60, 20) labelText:((FinderModel *)[Manager share].finderRecommendArray[3]).title textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:13]];
    
    [view addSubview:self.moneyBtn];
    [view addSubview:self.serverBtn];
    [view addSubview:self.activityBtn];
    [view addSubview:self.gameBtn];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 200;
}

// 唯一的代理方法  选中图片执行
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"点击选中的第%ld张.", index + 1);
}

// 按钮触发事件

- (void)shopBtnAction:(UIButton *)shopBtn
{
    switch (shopBtn.tag) {
        case rowOne:
            
            break;
        case rowTwo:
            
            break;
        case rowThree:
            
            break;
        case rowFour:
            
            break;
        case rowFive:
            
            break;
        case rowSix:
            
            break;
        case rowSeven:
            
            break;
        case rowEight:
            
            break;
            
            
        default:
            break;
    }
}

- (void)imageBtnAction:(UIButton *)imageBtn
{
    switch (imageBtn.tag) {
        case rowOne:
            
            break;
        case rowTwo:
            
            break;
        case rowThree:
            
            break;
        case rowFour:
            
            break;
        case rowFive:
            
            break;
        case rowSix:
            
            break;
        case rowSeven:
            
            break;
        case rowEight:
            
            break;
            
            
        default:
            break;
    }
}

- (void)buyBtnAction:(UIButton *)buyBtn
{
    NSLog(@"ad");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

/*
 - (void)subView
 {
 
 [self customButton:self.mongCityBtn image:nil imageFrame:CGRectMake(self.mongCityBtn.frame.size.width / 3 - 25, 5, 50, 50) labelFrame:CGRectMake(self.mongCityBtn.frame.size.width / 3 - 30, 55, 60, 20) labelText:@"金币商城" textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:13]];
 [self customButton:self.serverBtn image:nil imageFrame:CGRectMake(self.mongCityBtn.frame.size.width / 3 - 25, 5, 50, 50) labelFrame:CGRectMake(self.mongCityBtn.frame.size.width / 3 - 30, 55, 60, 20) labelText:@"身边服务" textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:13]];
 [self customButton:self.activityBtn image:nil imageFrame:CGRectMake(self.mongCityBtn.frame.size.width / 3 - 25, 5, 50, 50) labelFrame:CGRectMake(self.mongCityBtn.frame.size.width / 3 - 30, 55, 60, 20) labelText:@"活动广场" textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:13]];
 [self customButton:self.gameBtn image:nil imageFrame:CGRectMake(self.mongCityBtn.frame.size.width / 3 - 25, 5, 50, 50) labelFrame:CGRectMake(self.mongCityBtn.frame.size.width / 3 - 30, 55, 60, 20) labelText:@"游戏中心" textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:13]];
 
 self.tableView.delegate =self;
 self.tableView.dataSource = self;
 
 [self.tableView registerNib:[UINib nibWithNibName:@"FinderTableViewCell" bundle:nil] forCellReuseIdentifier:@"Findercell"];
 
 }
 
 #pragma mark -自定义Button
 // 该自定义button包含一个圆形image和一个label
 - (void)customButton:(UIButton *)button image:(UIImage *)image imageFrame:(CGRect)imageFrame labelFrame:(CGRect)labelFrame labelText:(NSString *)text textColor:(UIColor *)color textFont:(UIFont *)font
 {
 UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
 imageView.image = image;
 imageView.backgroundColor = [UIColor yellowColor];
 imageView.layer.cornerRadius = imageFrame.size.width / 2;
 imageView.layer.masksToBounds = YES;
 [button addSubview:imageView];
 UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
 label.text = text;
 label.textColor = color;
 label.textAlignment = NSTextAlignmentCenter;
 label.font = font;
 [button addSubview:label];
 }
 */

@end
